home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Commun⁄Network
/
NetNews Sample Code ƒ
/
News src
/
nntp.c
< prev
next >
Wrap
Text File
|
1990-10-12
|
2KB
|
136 lines
/*
* NNTP client routines.
*/
#include "nntp.h"
#define IPPORT_NNTP 119
/* tcp prototypes */
OSErr tcp_open(char *host, int port);
OSErr tcp_create(short size);
OSErr tcp_write(char *buf, int cnt);
OSErr tcp_read(char *buf, int *cnt);
OSErr tcp_close(void);
OSErr tcp_release(void);
void tcp_shutdown(void);
/*
* server_init Get a connection to the remote news server.
*
* Parameters: "machine" is the machine to connect to.
*
* Returns: -1 on error
* server's initial response code on success.
*
* Side effects: Connects to server.
*/
server_init(machine, imsg, isize)
char *machine, *imsg;
{
if (tcp_open(machine, IPPORT_NNTP) != noErr)
return -1;
/* Now get the server's signon message */
(void) get_server(imsg, isize);
return 0;
}
/*
* put_server -- send a line of text to the server
*
* Parameters: "string" is the string to be sent to the
* server.
*
* Returns: Nothing.
*
* Side effects: Talks to the server.
*
* Note: This routine flushes the buffer each time
* it is called. For large transmissions
* (i.e., posting news) don't use it. Instead,
* do the fprintf's yourself, and then a final
* fflush.
*/
void
put_server(string)
char *string;
{
tcp_write(string, strlen(string));
}
/*
* get_server -- get a line of text from the server. Strips
* CR's and LF's.
*
* Parameters: "string" has the buffer space for the
* line received.
* "size" is the size of the buffer.
*
* Returns: -1 on error, 0 otherwise.
*
* Side effects: Talks to server, changes contents of "string".
*/
#define IBUFSZ 1024
char ibuf[IBUFSZ], *ibufp;
int ibufcnt;
get_server(string, size)
char *string;
int size;
{
register char *cp;
register int c;
int cnt;
cp = string;
while (--size) {
if (ibufcnt <= 0) { /* need another read */
cnt = IBUFSZ;
if (tcp_read(ibuf, &cnt) != noErr)
return 0;
ibufcnt = cnt;
ibufp = ibuf;
}
c = *ibufp++;
ibufcnt--;
if (c == '\r')
continue;
if (c == '\n')
break;
*cp++ = c;
}
*cp = '\0';
return (0);
}
/*
* close_server -- close the connection to the server, after sending
* the "quit" command.
*
* Parameters: None.
*
* Returns: Nothing.
*
* Side effects: Closes the connection with the server.
* You can't use "put_server" or "get_server"
* after this routine is called.
*/
void
close_server()
{
char ser_line[256];
put_server("QUIT\r\n");
(void) get_server(ser_line, sizeof(ser_line));
}